/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * @brief map-messenger /map-messenger/include/MapSocket.h
 * Copyright (C) Matheus Neder, Amanda Duarte, Priscila Cardoso 2010
 * 
 * MapSocket.h is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MapSocket.h is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * @brief map-messenger /map-messenger/include/MapSocket.h
 * @file MapSocket.h
 * @date 14/06/2010
 * @author Matheus Neder <matheusneder@gmail.com>
 * @author Amanda Duarte <mandinhaduarte89@gmail.com>
 * @author Priscila Cardoso <japinha-htinha@hotmail.com>
 */

#ifndef MAPSOCKET_H_
#define MAPSOCKET_H_

#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <exception>
#include <stdexcept>
#include <string>
#include <cstdio>
#include <iostream>
#include "MapInteger.h"

using boost::asio::ip::tcp;
typedef boost::shared_ptr<tcp::socket> SocketSharedPtr;

class MapSocket
{
	tcp::socket* sock;
	boost::thread* thReader;
	bool isConnected;
	boost::asio::io_service io_service;

public:
	MapSocket() :
		sock(NULL), thReader(NULL), isConnected(false)
	{
	}

	void accept(uint port) throw (boost::system::system_error)
	{
//		boost::asio::io_service io_service;
		tcp::acceptor acc(io_service, tcp::endpoint(tcp::v4(), port));
		sock = new tcp::socket(io_service);
		acc.accept(*sock);
		isConnected = true;
	}

	void connect(const std::string& host, uint port)
			throw (boost::system::system_error)
	{
		static char strport[255];
		sprintf(strport, "%d", port);
//		boost::asio::io_service io_service;
		tcp::resolver resolver(io_service);
		tcp::resolver::query query(tcp::v4(), host, strport);
		tcp::resolver::iterator iterator = resolver.resolve(query);
		sock = new tcp::socket(io_service);
		sock->connect(*iterator);
		isConnected = true;
	}

	SocketSharedPtr getSocketSharedPtr()
	{
		return SocketSharedPtr(sock);
	}

	tcp::socket* getSocketPtr()
	{
		return sock;
	}

	inline bool getIsConnected() const
	{
		return isConnected;
	}

	static void read(SocketSharedPtr sock);

	static MapInteger plainRead(tcp::socket* sock);
	//	{
	//		using namespace std;
	//		const static int MAX_LENGTH = 1024;
	//		static char data[MAX_LENGTH];
	//		boost::system::error_code error;
	//		size_t length;
	//		try
	//		{
	//			for (;;)
	//			{
	//				length = sock->read_some(boost::asio::buffer(data), error);
	//				if (error == boost::asio::error::eof)
	//				{
	//					//TODO: exibir mensagem em caixa de dialogo
	//					cerr << "Connexao fechada" << endl;
	//					break; // Connection closed cleanly by peer.
	//				}
	//				else if (error)
	//					throw boost::system::system_error(error); // Some other error.
	//				//cout << data << endl;
	//				gui->chatAreaInsertMessage("Matheus", data);
	//			}
	//		} catch (boost::system::system_error& e)
	//		{
	//			//TODO: exibir mensagem em caixa de diagolo
	//			cerr << "Excecao na thread readSocket: " << e.what() << endl;
	//		}
	//	}

	static void write(tcp::socket* sock, const std::string& message);
	static void writeUsername(tcp::socket* sock, const std::string&);

	static void plainWrite(tcp::socket* sock, const MapInteger& message);
//	{
//		size_t length;
//		try
//		{
//			length = boost::asio::write(*sock, boost::asio::buffer(
//					message.c_str(), message.length()));
//			if (length != message.length())
//			{
//				throw std::runtime_error("Nao foi possivel escrever a mensagem");
//			}
//		} catch (std::exception& e)
//		{
//			//TODO: colocar mensagem em caixa de dialogo
//			std::cerr << "Excecao em writeSocket: " << e.what() << std::endl;
//		}
//	}
	static void readUsernameConnectedWith(tcp::socket* sock);
};

#endif /* MAPSOCKET_H_ */
